<?php
Class GalleryController{
public $title;
public $index;    

 
	public function __construct(){
		LoadHelpers("Use");
		
		require_once(LIBRARY_PATH.'/Kernel/Cache/Cache.php');
		$cache = DF_cache::Create()->SetResource('Object')->SetName('menu');
		
		$cat = $cache->Get();
		
		if($cat==null){
			
			$data = ORM::factory()->SetType('assoc')->prepare("SELECT GroupTovar.name, GroupTovar.url, CetegoryTovar.name as `category`, CetegoryTovar.url as `curl` FROM `GroupTovar` LEFT JOIN `CetegoryTovar` ON GroupTovar.id = CetegoryTovar.group")->getResult();
			
			$cat = array();
			
			foreach($data as $item){
				$cat[$item['url']][] = array('name'=>$item['name'],'category'=>$item['category'],'curl'=>$item['curl']);
			}
			$cache->SetData($cat);
			$cache->SetResource('Object')->SetName('menu')->Save();
		}
		$this->cat = $cat;
		
		unset($data);
		unset($cat);
		/*echo"<pre>";
		print_r($this->cat);
		echo"</pre>";*/
		
		$this->new_goods = ORM::factory()->SetType('assoc')->prepare("SELECT goods.id, goods.url, goods.name, goods.small, GroupTovar.url as `group`, CetegoryTovar.url as `cat` FROM `goods`, `GroupTovar`, `CetegoryTovar` WHERE goods.Sub=CetegoryTovar.id AND CetegoryTovar.group = GroupTovar.id LIMIT 0,5")->getResult();
	}


	public function IndexAction(){
		$this->title = "Фотогалерея";

		$this->gallery = ORM::factory('album')->Select("url, prev, desc")->SetType('assoc')->OrderBy("sort","ASC")->find_all();
		/*echo"<pre>";
		print_r($this->gallery);
		echo"</pre>";*/
		$array = array(
			array('link'=>'index','name'=>'Головна'),
			array('link'=>'gallery','name'=>'Фотогалерея')
		);
		$this->link = LinkGen::Link($array, 'block', 'sitemap');
    }
    
    public function AlbumAction(){
		$request = getRequest::getInstance();
		$album = $request->getVar('name', 'GET');
		
		(int)$page = $request->getVar('id', 'GET');
		
		$array = array();
		$array[] = array('link'=>'index','name'=>'Головна');
		$array[] = array('link'=>'gallery','name'=>'Фотогалерея');
	//if(!empty($razdel)){
		//$count = ORM::factory('gallery')->Where("album", $razdel["0"]->id)->_count();
		//$this->pager = Pager::Create($count, "/gallery/album.html?name=".$album."&page=");
		
		//$this->gallery = ORM::factory('gallery')->SetType('assoc')->Where("album", $razdel["0"]->id)->Limit($this->pager['limit'])->OrderBy("id","ASC")->find_all();
		$this->gallery = ORM::factory()->prepare("SELECT gallery.full, gallery.prev, gallery.descrip, album.desc FROM `gallery`,`album` WHERE gallery.album = album.id AND album.url='".$album."'")->getResult();
		/*echo"<pre>";
		print_r($this->gallery);
		echo"</pre>";*/
		if(!empty($this->gallery)){
			$this->title = 'Фотогалерея - '.$this->gallery[0]['desc'];
			$array[] = array('link'=>'gallery/album','name'=>$this->gallery[0]['desc'], 'param'=>$album, 'type'=>'name');
		}else{
			$this->gallery = array();
			$this->pager = array();
			$this->title = "Фотоальбом не знайдено!";
		}

		$this->link = LinkGen::Link($array, 'block', 'navig', true);
	}
}
